package com.example.e_comm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.service.PermissionService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/permission")
public class PermissionController {
    private PermissionService permissionService;
    private PermissionService UserService;

    public PermissionController(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    @GetMapping("/list")
    public List<Permission> list() {
        return permissionService.list();
    }

    @GetMapping("/listByPage")
    public List<Permission> listByPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize
    ) {
        Page<Permission> page = new Page<>(pageNum, pageSize);
        return permissionService.getPermissionByPage(page);
    }

    @GetMapping("/listByPageWithCondition")
    public List<Permission> listByPageWithCondition(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String keyword
    ) {
        Page<Permission> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
        if (!keyword.isEmpty()) {
            queryWrapper.like("perm_name", keyword).or().like("perm_key", keyword);
        }
        return permissionService.page(page, queryWrapper).getRecords();
    }

    @GetMapping("/{id}")
    public Permission getById(@PathVariable Long id) {
        return permissionService.getById(id);
    }

    @PostMapping("/create")
    public boolean create(@RequestBody Permission permission) {
        if (permissionService.count(
                new QueryWrapper<Permission>()
                        .eq("perm_name", permission.getPermName())) > 0) {
            return false;
        }
        if (permissionService.count(
                new QueryWrapper<Permission>()
                        .eq("perm_key", permission.getPermKey())) > 0) {
            return false;
        }
        return permissionService.save(permission);
    }

    @PutMapping("/update")
    public boolean update(@RequestBody Permission permission) {
        return permissionService.updateById(permission);
    }

    @PostMapping("/delete/ids")
    public boolean delete(@RequestBody List<Long> ids) {
        return permissionService.removeByIds(ids);
    }

    /***
     * 获取用户获取其权限列表
     * @param roleId
     * @return 权限列表
     */
    @GetMapping("/listPermissionByRoleId")
    public List<Permission> getPermissionByRoleId(@RequestParam Long roleId) {
        // 角色是否存在
        if (permissionService.getById(roleId) == null) {
            throw new RuntimeException("角色不存在");
        }
        return permissionService.getPermissionByRoleId(roleId);
    }
    @GetMapping("/listPermissionByUserId")
    public Set<String> getPermissionIdsByUserId(@RequestParam Long userId) {
        //角色是否存在
        if(permissionService.getById(userId)==null){
            throw new RuntimeException("角色不存在");
        }
        return permissionService.getPermissionByUserIdSet(userId);
    }
}